/*
 * Copyright 2008-2012 Centro Andaluz de Innovación y Tecnologías de la
 * Información y las Comunicaciones (CITIC). All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Please contact CITIC - Edificio CITIC, C/ Marie Curie, 6 Parque
 * Tecnológico de Andalucía 29590 - Campanillas, Málaga, Spain
 * or visit www.citic.es, if you need additional information or
 * have any questions.
 */

package es.citic.puedes.youtube.testhelp;

import java.util.Iterator;
import java.util.List;

import es.citic.puedes.youtube.IYouTubeManager;
import es.citic.puedes.youtube.YouTubeAuthException;
import es.citic.puedes.youtube.YouTubeException;
import es.citic.puedes.youtube.model.YouTubeUser;
import es.citic.puedes.youtube.model.YouTubeVideoMinimal;
import es.citic.puedes.youtube.test.YouTubeManagerTest;

public class YouTubeFavoriteTest {

	public static void runYouTubeGetFavoriteTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}
		
		// gets favorites from an user
		System.out.println("GETS FAVORITES.. " );
		YouTubeUser userProfile = ytm.getUserProfile(userId);
		List<YouTubeVideoMinimal> favoriteVideoList = ytm.getFavorites(userProfile);
		YouTubePrinterTestMethods.printModelVideoMinimalList(favoriteVideoList, "FAVORITES:");
	}

	public static void runYouTubeAddFavoriteTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}
		
		YouTubeUser userProfile = ytm.getUserProfile(userId);

		//adds a video to favorites
		System.out.println("adds favorite.. " );
		ytm.addFavorites(userProfile, videoID);
	}

	public static void runYouTubeDeleteFavoriteTest(IYouTubeManager ytm, Integer userId, String videoID) throws SecurityException, YouTubeException, YouTubeAuthException {
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}
				
		YouTubeUser userProfile = ytm.getUserProfile(userId);
		
		// ADD a favorite video
		try {
			ytm.addFavorites(userProfile, videoID);
		} catch (Exception e) {
			//Nothing to do
		}
		
		List<YouTubeVideoMinimal> favoriteVideoList = ytm.getFavorites(userProfile);
		Iterator<YouTubeVideoMinimal> iter = favoriteVideoList.iterator();
		YouTubeVideoMinimal favoriteVideo = null;
		while (iter.hasNext()){
			favoriteVideo = iter.next();
			if (favoriteVideo.getVideoId().equals(videoID)){
				return;
			}
		}
		
		// deletes a favorite video from an user
		System.out.println("deletes favorite.. " );
		ytm.deleteFavorites(userProfile, favoriteVideo.getVideoId());
	}

	public static void runYouTubeGetFavoritePaginationTest(IYouTubeManager ytm, Integer userId) throws SecurityException, YouTubeException, YouTubeAuthException {		
		try {
			ytm.registreUser(userId, YouTubeManagerTest.username, YouTubeManagerTest.pass);
		} catch (Exception e) {
			// Nothing to do
		}

		YouTubeUser userProfile = ytm.getUserProfile(userId);

		//gets favorites from an user by paging
		System.out.println("gets favorites pagination.. " );
		List<YouTubeVideoMinimal> favoriteVideoList  = ytm.getFavorites(userProfile,1,4);
		YouTubePrinterTestMethods.printModelVideoMinimalList(favoriteVideoList, "FAVORITES BY PAGING:");
	}

}
